package com.frxuexi.park2209.visitor

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.base.net.Const
import com.frxuexi.park2209.R
import com.frxuexi.park2209.databinding.ActivityAddVisitorBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.Calendar

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding,VisitorViewModel>() {


    override fun initData() {
        lifecycleScope.launch {
            vm.addVisitorSuc.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong(it)
                    finish()
                }
            }
        }
    }

    override fun initView() {
        binding.visitorStartTime.text=getDate(System.currentTimeMillis())
        binding.visitorEndTime.text=getDate(System.currentTimeMillis())

        binding.visitorIvstart.setOnClickListener {
            showDialog(binding.visitorStartTime)
        }

        binding.visitorIvend.setOnClickListener {
            showDialog(binding.visitorEndTime)
        }


        binding.btnVisitorcommit.setOnClickListener {
            val map = mutableMapOf<String, Any>()
            map["carNum"]= binding.visitorCarNo.text.toString()
//            map["parkId"]: 0,
            map["userId"]= 0
            map["visitorDescribe"]=binding.visitorSelColor.text.toString()
            map["visitorEnd"]=binding.visitorEndTime.text.toString()
            map["visitorStart"]= binding.visitorStartTime.text.toString()
            map["visitorTime"]=getDataTime(System.currentTimeMillis())
            vm.addVisitor(map)
        }

    }

    fun showDialog(tv: TextView){
        val calendar = Calendar.getInstance()
        DatePickerDialog(this,object : DatePickerDialog.OnDateSetListener {
            override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                tv.text="$year-${month+1}-$dayOfMonth"
                var num=binding.visitorEndTime.text.last().toInt()-binding.visitorStartTime.text.last().toInt()+1
                binding.visitorAuthorName.text="${num}天"
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
    }
}